//
//  FGYInputView.m
//  01 - shop
//
//  Created by ma c on 16/8/18.
//  Copyright (c) 2016年 beijing. All rights reserved.
//

#import "FGYInputView.h"

@interface FGYInputView ()<UITextFieldDelegate>

@property (strong, nonatomic) UILabel * promptLabel; //提示label
@property (strong, nonatomic) UITextField * phoneNumTextField;//输入手机号
@property (strong, nonatomic) UITextField * passWordTextField;//输入密码
@property (strong, nonatomic) UIButton * nextBtn;//下一步按钮
@property (strong, nonatomic) UIButton * loginBtn;//去登陆按钮
@property (strong, nonatomic) UILabel * backGroundLabel;//装输入框的Label;
@property (strong, nonatomic) UILabel * lineLabel;//分割线的label

@end
@implementation FGYInputView

- (UILabel *)lineLabel{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc]init];
        _lineLabel.backgroundColor = RGB(232, 231, 233);
    }
    return _lineLabel;
}

- (UILabel *)backGroundLabel{
    if (!_backGroundLabel) {
        _backGroundLabel = [[UILabel alloc]init];
        _backGroundLabel.backgroundColor = [UIColor whiteColor];
        
    }
    return _backGroundLabel;
}

- (UILabel *)promptLabel{
    if (!_promptLabel) {
        _promptLabel = [[UILabel alloc]init];
        _promptLabel.text = @"请输入手机号码注册新用户";
        _promptLabel.textColor = RGB(126, 126, 126);
    }
    return _promptLabel;
}

- (UITextField *)phoneNumTextField{
    if (!_phoneNumTextField) {
        _phoneNumTextField = [[UITextField alloc]init];
        _phoneNumTextField.placeholder = @"请输入手机号";
    }
    [_phoneNumTextField addTarget:self action:@selector(phoneNumTextChange:) forControlEvents:UIControlEventEditingChanged];
    _phoneNumTextField.delegate = self;
    return _phoneNumTextField;
}

- (UITextField *)passWordTextField{
    if (!_passWordTextField) {
        _passWordTextField = [[UITextField alloc]init];
        _passWordTextField.placeholder = @"请设置密码";
    }
    _passWordTextField.delegate = self;
    [_passWordTextField addTarget:self action:@selector(passWordTextChange:) forControlEvents:UIControlEventEditingChanged];
    return _passWordTextField;
}

- (UIButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextBtn.backgroundColor = RGB(234, 234, 234);
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        UIColor  * color = RGB(150, 150, 150);
        [_nextBtn setTitleColor: color forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];
        _nextBtn.userInteractionEnabled = NO;
    }
    return _nextBtn;
}

- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"去登陆" forState:UIControlStateNormal];
        UIColor  * color = RGB(0, 178, 239);
        [_loginBtn setTitleColor:color forState:UIControlStateNormal];
    }
    return _loginBtn;
}

- (void)pushController{
    self.blockWithDict(@{@"phoneNume":_phoneNumTextField.text,@"passWord":_passWordTextField.text});
}

- (void)phoneNumTextChange:(UITextField *)textField{
    if (textField.text.length == 11 && _passWordTextField.text.length > 5) {
        _nextBtn.backgroundColor = RGB(0, 183, 240);
        [_nextBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _nextBtn.userInteractionEnabled = YES;
    }else{
        _nextBtn.backgroundColor = RGB(234, 234, 234);
        UIColor  * color = RGB(150, 150, 150);
        [_nextBtn setTitleColor: color forState:UIControlStateNormal];
        _nextBtn.userInteractionEnabled = NO;
    }
}

- (void)passWordTextChange:(UITextField *)textField{
    if (textField.text.length > 5 && _phoneNumTextField.text.length == 11) {
        _nextBtn.backgroundColor = RGB(0, 183, 240);
        [_nextBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _nextBtn.userInteractionEnabled = YES;
    }else{
        _nextBtn.backgroundColor = RGB(234, 234, 234);
        UIColor  * color = RGB(150, 150, 150);
        [_nextBtn setTitleColor: color forState:UIControlStateNormal];
        _nextBtn.userInteractionEnabled = NO;
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.phoneNumTextField) {
        if (range.location > 10) {
            return NO;
        }
    }else if (textField == self.passWordTextField){
        if(range.location > 5){
            return NO;
        }
    }
    

    return YES;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.promptLabel];
        [self addSubview:self.backGroundLabel];
        [self addSubview:self.phoneNumTextField];
        [self addSubview:self.passWordTextField];
        [self addSubview:self.lineLabel];
        [self addSubview:self.nextBtn];
        [self addSubview:self.loginBtn];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    [_promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.mas_top).offset(11);
        make.height.equalTo(@14);
    }];
    
    [_backGroundLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.promptLabel.mas_bottom).offset(11);
        make.left.right.equalTo(weakSelf);
        make.height.equalTo(@90);
    }];
    
    [_phoneNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.backGroundLabel.mas_left).offset(15);
        make.top.equalTo(weakSelf.backGroundLabel.mas_top);
        make.right.equalTo(weakSelf.backGroundLabel.mas_right);
        make.height.equalTo(@44);
        
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.phoneNumTextField.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.phoneNumTextField.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    [_passWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.phoneNumTextField.mas_left);
        make.top.equalTo(weakSelf.lineLabel.mas_bottom);
        make.right.equalTo(weakSelf.mas_right);
        make.height.equalTo(@44);
        
    }];
    
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.top.equalTo(weakSelf.passWordTextField.mas_bottom).offset(16);
        make.height.equalTo(@35);
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.mas_right).offset(10);
        make.top.equalTo(weakSelf.nextBtn.mas_bottom).offset(23);
        make.size.mas_equalTo(CGSizeMake(100, 17));
    }];
    
    
}

@end
